﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;

namespace ArtLand.OA.Project.HR
{


    public partial class HR_ContractChange : CommonPageBase
    {
        List<TempFile> files = new List<TempFile>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                BindGrid();
            }
        }

        public void LoadData()
        {
            int id = 0;
            if (!string.IsNullOrEmpty(Request["conid"]))
            {
                id = int.Parse(Request["conid"]);
                HR_CONTRACT _con = TbsDbSession.Default.From<HR_CONTRACT>().Where(HR_CONTRACT._.ID == id).ToFirst();
                txt_DIRMGR.Text = _con.DIRMGR;
                txt_HRMMGR.Text = _con.HRMMGR;
                txt_EMPLID.Text = _con.EMPLID;
                txt_ENTRYTIME.Text = _con.ENTRYTIME.Value.ToString("yyyy-MM-dd");
                if (_con.PositiveTime.HasValue)
                    txt_PositiveTime.Text = _con.PositiveTime.Value.ToString("yyyy-MM-dd");
                txt_JOIDAT.Text = _con.JOIDAT.Value.ToString("yyyy-MM-dd");
                txt_ENDDAT.Text = _con.ENDDAT.Value.ToString("yyyy-MM-dd");
                Drp_CEKSAT.SelectedValue = _con.CEKSAT.Value.ToString();
                Drp_StaffStatus.SelectedValue = _con.StaffStatus.Value.ToString();
                txt_REMARK.Text = _con.REMARK;
                txt_POST.Text = _con.POST;
                hf.Text = _con.EMDOCT;
                txt_EMPLID.Enabled = false;
            }
            else
            {
                if (!string.IsNullOrEmpty(Request["empid"]))
                {
                    txt_EMPLID.Text = Request["empid"];
                }
            }
            
        }

        protected bool ValidateForms()
        {
            bool f = true;
            string message = "";
            if (txt_DIRMGR.Text == "")
            {
                message = "请输入直接经理编号";
                Alert.Show(message);
                txt_DIRMGR.MarkInvalid(message);
                return false;
            }
            if (txt_ENTRYTIME.Text == "")
            {
                message = "请输入入职日期";
                Alert.Show(message);
                txt_ENTRYTIME.MarkInvalid(message);
                return false;
            }
            if (txt_JOIDAT.Text == "")
            {
                message = "请输入合同开始日期";
                Alert.Show(message);
                txt_JOIDAT.MarkInvalid(message);
                return false;
            }
            if (txt_ENDDAT.Text == "")
            {
                message = "请输入合同结束日期";
                Alert.Show(message);
                txt_ENDDAT.MarkInvalid(message);
                return false;
            }
            if (txt_POST.Text == "")
            {
                message = "请输入岗位";
                Alert.Show(message);
                txt_POST.MarkInvalid(message);
                return false;
            }
            return f;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateForms())
            {
                int returnvalue = 0;
                HR_CONTRACT hc = new HR_CONTRACT();
                if (!string.IsNullOrEmpty(Request["conid"]))
                {
                    hc.ID = int.Parse(Request["conid"]);
                    hc.Attach();
                    hc.CEKSAT = int.Parse(Drp_CEKSAT.SelectedValue);
                    hc.DIRMGR = txt_DIRMGR.Text;
                    hc.EMDOCT = hf.Text;
                    hc.EMPLID = txt_EMPLID.Text;
                    hc.ENDDAT = Convert.ToDateTime(txt_ENDDAT.Text);
                    hc.ENTRYTIME = Convert.ToDateTime(txt_ENTRYTIME.Text);
                    hc.JOIDAT = Convert.ToDateTime(txt_JOIDAT.Text);
                    if (txt_PositiveTime.Text != "")
                        hc.PositiveTime = Convert.ToDateTime(txt_PositiveTime.Text);
                    hc.POST = txt_POST.Text;
                    hc.REMARK = txt_REMARK.Text;
                    hc.StaffStatus = int.Parse(Drp_StaffStatus.SelectedValue);
                    hc.HRMMGR = txt_HRMMGR.Text;
                    hc.EMCONT = txt_EMCONT.Text;
                    returnvalue = TbsDbSession.Default.Update<HR_CONTRACT>(hc);
                }
                else
                {
                    hc.DeAttach();
                    hc.CEKSAT = int.Parse(Drp_CEKSAT.SelectedValue);
                    hc.DIRMGR = txt_DIRMGR.Text;
                    hc.EMDOCT = hf.Text;
                    hc.EMPLID = txt_EMPLID.Text;
                    hc.ENDDAT = Convert.ToDateTime(txt_ENDDAT.Text);
                    hc.ENTRYTIME = Convert.ToDateTime(txt_ENTRYTIME.Text);
                    hc.JOIDAT = Convert.ToDateTime(txt_JOIDAT.Text);
                    if (txt_PositiveTime.Text != "")
                        hc.PositiveTime = Convert.ToDateTime(txt_PositiveTime.Text);
                    hc.POST = txt_POST.Text;
                    hc.REMARK = txt_REMARK.Text;
                    hc.HRMMGR = txt_HRMMGR.Text;
                    hc.EMCONT = txt_EMCONT.Text;
                    hc.StaffStatus = int.Parse(Drp_StaffStatus.SelectedValue);
                    returnvalue = TbsDbSession.Default.Insert<HR_CONTRACT>(hc);
                }
                if (returnvalue > 0)
                {
                    Alert.Show("保存成功！", "提示", ActiveWindow.GetHideRefreshReference());
                }
            }
        }


        protected void hibtn_Click(object sender, EventArgs e)
        {
        }

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            if (filePhoto.HasFile)
            {
                string fileName = filePhoto.ShortFileName;
                String[] str = { "1", "2", "3", "4", "5", "6", "7" };
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

                filePhoto.SaveAs(Server.MapPath("~/upload/" + fileName));
                hf.Text += fileName + ";";
                // 清空文件上传组件
                filePhoto.Reset();
                BindGrid();
            }
        }

        protected void BindGrid()
        {
            List<string> ls = StringHelper.GetSubStringList(hf.Text, ';');
            foreach (string item in ls)
            {
                TempFile tf = new TempFile();
                tf.FileName = item;
                files.Add(tf);
            }
            Grid1.DataSource = files;
            Grid1.DataBind();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string[] str = Grid1.Rows[e.RowIndex].Values;
                hf.Text = hf.Text.Replace(str[1], "");
                BindGrid();
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            e.Values[0] = "<a href=\"/upload/" + e.Values[0] + "\">" + e.Values[0] + "</a>";
        }
    }
}